FinishTemplateTaskForm = function(config) {

    // Privatne varijable
    var privateVariable = '';

    this.valueData = {};
    this.formConfig = {};

    // Konfigruacija Window objekta se nalazi u this
    Ext.apply(this, config || {}, {
        title: 'Završi uređivanje predloška'
    });

    // Konfigruacija FormPanel objekta se nalazi u this.formConfig
    Ext.applyIf(this.formConfig, {
        monitorValid:true,
        fileUpload : true,
        items : [{
                    xtype: 'button',
                    fieldLabel: 'Trenutna verzija',
                    anchor: '0%',
                    text: 'Download',
                    handler : this.doDownload,
                    scope : this
                }, {
                    xtype: 'fileuploadfield',
                    emptyText: 'Odaberi datoteku...',
                    fieldLabel: 'Nova verzija',
                    name: 'fileData',
                    buttonText: '',
                    buttonCfg: {
                        iconCls: 'button-browseFile'
                    }
                }, {
                    xtype : 'hidden',
                    name : 'bpmn_packageId'
                }, {
                    xtype : 'hidden',
                    name : 'taskId'
                }, {
                    xtype : 'hidden',
                    name : 'contextUrl'
                }
        ],
        buttons: [{
                    text : 'Odustani',
                    handler :this.close,
                    scope : this
                }, {
                    text : 'Upload',
                    handler: this.doUpload,
                    scope : this
                }, {
                    text : 'Završi',
                    handler : this.doFinish,
                    scope : this
                }, {
                    text : 'Obriši',
                    handler : this.doDelete,
                    scope : this
                }
        ]
    });

    FinishTemplateTaskForm.superclass.constructor.call(this);

};

Ext.extend(FinishTemplateTaskForm, AbstractForm, {

    doDownload : function() {
        var contextUrl = this.getForm().findField('contextUrl').getValue();
        var packageId = this.getForm().findField('bpmn_packageId').getValue();
        var url = contextUrl + "/document?id=" + packageId;
        window.open(url);
    },

    doUpload : function() {
        var fileField = this.getForm().findField('fileData');
        var id = this.getForm().findField('bpmn_packageId');
        if (fileField.getValue() === undefined || fileField.getValue() === '') {
            fileField.markInvalid("Ovo polje je obavezno!");
        }
        else {
            this.getForm().submit( {
                url : 'mvc/document',
                method : 'POST',
                scope : this,
                params : {
                    id : id.getValue()
                },
                success : function(){
                    this.getForm().findField('fileData').reset();
                    MessageBoxHandler.showInfoBox('Datoteka uspješno uploadana.');
                },
                //failure : function(){alert("Greška!");},
                waitTitle : 'Molim pričekajte',
                waitMsg : 'Spremam!',
                submitEmptyText : false,
                clientValidation : false
            });
        }
    },

    doFinish : function(){
        var fileField = this.getForm().findField('fileData');
        if (fileField.getValue() === undefined || fileField.getValue() === '') {
            var question = 'Niste dodali novu verziju predloška. Jeste li sigurni da želite postojeću verziju poslati na odobrenje?';
            Ext.MessageBox.confirm('Confirm', question, function(btn){
                if (btn === 'yes') {
                    this.formSubmit({'bpmn_action':'finish'},function(){this.close();});
                }
            }, this);
        }
        else {
            this.formSubmit({'bpmn_action':'finish'},function(){this.close();});
        }
    },

    doDelete : function(){
        var question = 'Jeste li sigurni da želite obrisati predložak?';
        Ext.MessageBox.confirm('Confirm', question, function(btn){
            if (btn === 'yes') {
                this.formSubmit({'bpmn_action':'delete'},function(){this.close();});
            }
        }, this);
    },

    formSubmit : function(params, successCallback){
        this.getForm().submit( {
            url : 'mvc/task/form',
            method : 'POST',
            scope : this,
            params : params,
            success : successCallback,
            //failure : function(){alert("Greška!");},
            waitTitle : 'Molim pričekajte',
            waitMsg : 'Spremam!',
            submitEmptyText : false,
            clientValidation : false
        });
    }

});

Ext.reg('finishTemplateTaskForm', FinishTemplateTaskForm);